#include <iostream>
#include <streambuf>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <memory>

#include <QDebug>
#define debugx1 qDebug().noquote() <<__FILE__<<__LINE__ <<Q_FUNC_INFO
#define debugx(...) do{debugx1;qDebug(__VA_ARGS__);}while(0)

using namespace std;
class mybuf : public streambuf
{
public:
    enum{ SIZE = 10};
    mybuf()
    {
        memset(buffer, 'j', SIZE);
        //buffer[3] = ' ';
        setbuf(buffer, SIZE);
    }
    void log()
    {
        cout <<hex<<gptr() << endl;
    }
protected:
    int_type overflow( int_type c)override
    {
        debugx("overflow" );
        return c;
    }
    streambuf* setbuf(char* s, streamsize n)override
    {
        setp(s, s + n);
        setg(s, s, s + n);
        return this;
    }
    int_type underflow() override
    {
        debugx("underflow" );
        memset(buffer, 'w', 10);
        setg(buffer, buffer, buffer+10);
        return ' ';
    }
    int_type uflow() override
    {
        debugx("uflow" );
        memset(buffer, 'x', 10);
        setg(buffer, buffer, buffer + 10);
        return EOF;
    }
private:
    char buffer[SIZE];
};
void streamBufDemo()
{
    mybuf buf;
    char test[2000];
    memset(test, 'a', 2000);
    //buf.pubsetbuf(test, 1000);
    string hh;
    string xx;
    istream in(&buf);
    ostream tt(&buf);
    in>>hh;
    qDebug()<< "hh: "  << hh ;
    //tt.write(test, 9);
    in >> xx;
    in.read(test, 11);
    qDebug()<< "xx: "  << xx ;
}
